﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.App_Code;
using QXBook.Utils;
using System.Configuration;
using QXBook.Model;
using QXBook.Service;

namespace Web.SysManage
{
    public partial class Login : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.ltitle.Text = System.Configuration.ConfigurationManager.AppSettings["WebTitle"];
                this.month.Text = DateTime.Now.Month.ToString();

                HttpCookie lname = QXBook.Utils.Cookie.Get(System.Configuration.ConfigurationManager.AppSettings["AdminCookieName"]);
                if (lname != null && lname.Values["radminName"] != null)
                {
                    this.rUsername.Checked = true;
                    this.username.Value = HttpContext.Current.Server.UrlDecode(lname.Values["radminName"].ToString());
                }

                ServiceFactory.CreateInstance<AdminService>().AminInfoRefash();
            }
            else
            {
                string work = CommonUtils.Post("Work");

                switch (work)
                {
                    case "LOGIN":
                        this.LoginMethod();
                        break;
                }

                return;
            }
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        private void LoginMethod()
        {
            string adminname = CommonUtils.Post("username");
            string password = CommonUtils.Post("password");
            string anme = CommonUtils.Post("rUsername");

            bool ranme = anme == "checkbox" ? true : false;

            if (string.IsNullOrEmpty(adminname) || string.IsNullOrEmpty(password))
            {
                base.AjaxErch("-1");
                return;
            }

            HttpCookie lname = QXBook.Utils.Cookie.Get(System.Configuration.ConfigurationManager.AppSettings["AdminCookieName"]);
            if (ranme)
            {
                if (lname == null) lname = QXBook.Utils.Cookie.Set(System.Configuration.ConfigurationManager.AppSettings["AdminCookieName"]);
                lname.Values["radminName"] = HttpContext.Current.Server.UrlEncode(adminname);
                lname.Expires = DateTime.Now.AddMonths(1);
                QXBook.Utils.Cookie.Save(lname);
            }
            else
            {
                if (lname != null) QXBook.Utils.Cookie.Remove(lname);
            }

            string response = "";

            string rtn = string.Empty;
            try
            {
                rtn = ServiceFactory.CreateInstance<AdminService>().AdminLogin(adminname, password);

            }
            catch (Exception ex)
            {
                base.ajaxdata = "{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}";
                base.AjaxErch(base.ajaxdata);
                return;
            }

            if (rtn != MsgConst.OK)
            {
                response = "{state:false,message:'" + rtn + "'}";
            }
            else
            {
                response = "{state:true}";
            }

            base.AjaxErch(response);
        }
    }
}